package com.atguigu.edu.publisher.controller;

import com.atguigu.edu.publisher.beans.UserChangeCtPerType;
import com.atguigu.edu.publisher.beans.UserPageCt;
import com.atguigu.edu.publisher.service.UserStatsService;
import com.atguigu.edu.publisher.utils.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
//localhost:8070/uvPerPage?date=20221021
//localhost:8070/userChangeCt?date=20221021
@RestController
public class UserStatsController {
    @Autowired
    private UserStatsService userStatsService;

    @RequestMapping("/uvPerPage")
    public String getUvPerPage(@RequestParam(value="date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<UserPageCt> pageCtList = userStatsService.getUvByPage(date);
        StringBuilder rows = new StringBuilder("[");
        for (int i = 0; i < pageCtList.size(); i++) {
            UserPageCt userPageCt = pageCtList.get(i);
            String pageId = userPageCt.getPageId();
            Integer uvCt = userPageCt.getUserCt();
            rows.append("{\n" +
                    "        \"page_id\": \"" + pageId + "\",\n" +
                    "        \"uv_ct\": \"" + uvCt + "\"\n" +
                    "      }");
            if (i < pageCtList.size() - 1) {
                rows.append(",");
            } else {
                rows.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"页面\",\n" +
                "        \"id\": \"page_id\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"独立访客数\",\n" +
                "        \"id\": \"uv_ct\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + rows + "\n" +
                "  }\n" +
                "}";

    }

    @RequestMapping("/userChangeCt")
    public String getUserChange(@RequestParam(value="date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<UserChangeCtPerType> userChangeCtList = userStatsService.getUserChangeCt(date);
        StringBuilder rows = new StringBuilder("[");
        for (int i = 0; i < userChangeCtList.size(); i++) {
            UserChangeCtPerType userChangeCt = userChangeCtList.get(i);
            String type = userChangeCt.getType();
            Integer userCt = userChangeCt.getUserCt();
            rows.append("{\n" +
                    "\t\"type\": \"" + type + "\",\n" +
                    "\t\"user_ct\": \"" + userCt + "\"\n" +
                    "}");
            if (i < userChangeCtList.size() - 1) {
                rows.append(",");
            } else {
                rows.append("]");
            }
        }

        return "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"columns\": [\n" +
                "      {\n" +
                "        \"name\": \"变动类型\",\n" +
                "        \"id\": \"type\"\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"用户数\",\n" +
                "        \"id\": \"user_ct\"\n" +
                "      }\n" +
                "    ],\n" +
                "    \"rows\": " + rows + "\n" +
                "  }\n" +
                "}";
    }
}
